package com.mcxx.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.system.service.IndexPageService;
import com.mcxx.modules.system.entity.CommonUpdateTipsEntity;
import com.mcxx.modules.system.service.read.CommonUserTipsLogReadService;
import com.mcxx.modules.system.service.write.CommonUserTipsLogWriteService;
import com.mcxx.redis.client.RedisClusterUtils;
import com.mcxx.service.DefaultRestService;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import dm.jdbc.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;


@Validated
@Api("首页")
@RestService({"indexPage/v2"})
public class IndexPageController extends DefaultRestService {

    private final static Logger logger = LoggerFactory.getLogger(IndexPageController.class);

    @Autowired
    private IndexPageService indexPageService;
    @Autowired
    private CommonUserTipsLogReadService commonUserViewLogReadService;
    @Autowired
    private CommonUserTipsLogWriteService commonUserTipsLogWriteService;

    @Autowired
    private RedisClusterUtils redisClusterUtils;


    @GetMapping("getOrderSummary")
    @ApiOperation("统计所有待办业务")
    public void getOrderSummary() {
        logger.info("统计所有待办业务");
        Map<String, Integer> orderSummary = indexPageService.getOrderSummary(getCurUserAccount());
        setData(orderSummary);
        logger.info("统计所有待办业务完成，结果：{}", JSON.toJSONString(orderSummary));
    }


    @GetMapping("getSystemUpdateTips")
    @ApiOperation(value="获取系统更新信息",notes="获取系统更新信息")
    public void getSystemUpdateTips()  {
        String userId = getCurUserId();
        logger.info("获取系统更新信息,参数：{}",userId);
        CommonUpdateTipsEntity entity = commonUserViewLogReadService.selectSystemUpdateInfo(userId);
        setData(entity);
        logger.info("获取系统更新信息结束,结果：{}",entity);
    }

    @GetMapping("insertUserViewLog")
    @ApiOperation(value="添加用户查看系统更新日志记录",notes="获取系统更新信息")
    public void insertUserViewLog(String tipsId)  {
        String userId = getCurUserId();
        logger.info("添加用户查看系统更新日志记录,参数：{},{}",tipsId,userId);
        int i = commonUserTipsLogWriteService.insert(userId,tipsId);
        logger.info("添加用户查看系统更新日志记录,结果：{}",i);
    }


    @GetMapping("getFinanciaHint")
    @ApiOperation(value="从Redis中取出财政是否更新的标识",notes="获取系统更新信息")
    public void getFinanciaHint(@RequestParam("userId") String userId, @RequestParam("areaCode") String areaCode){
        logger.info("从Redis中取出财政更新信息");

        if(StringUtil.isEmpty(userId) || StringUtil.isEmpty(areaCode)){
            return;
        }
        String tag = (String)redisClusterUtils.hget(areaCode, userId);
        if (tag != null && !ObjectUtils.isEmpty(tag)) {
            redisClusterUtils.hdel(areaCode,userId);
            setData(tag);
        }else{
            return;
        }
    }

}
